<?php

namespace App\Http\Controllers\Callcenter;

use App\Http\Controllers\Controller;
use App\Models\Customer;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\View;

class CustomerController extends Controller
{

    public function create(Request $request)
    {
        $phone = $request->input('phone');
        return View::make('callcenter.customer.create',compact('phone'));
    }

    public function store(Request $request)
    {
        $user = $request->user();
        $data = $request->all(['name', 'contact_name', 'contact_phone', 'type']);
        try {
            Customer::create([
                'uuid' => create_num('kh'),
                'name' => $data['name'],
                'contact_name' => $data['contact_name'],
                'contact_phone' => $data['contact_phone'],
                'type' => $data['type'],
                'created_user_id' => $user->id,
                'created_user_nickname' => $user->nickname,
                'owner_user_id' => $user->id,
                'owner_user_nickname' => $user->nickname,
                'owner_department_id' => $user->department_id ?? 0,
                'assignment_user_id' => $user->id,
                'assignment_user_nickname' => $user->nickname,
                'status' => 3,
                'source' => 8,
                'status_time' => date('Y-m-d H:i:s'),
            ]);
            return $this->success();
        } catch (\Exception $exception) {
            Log::error('客户录入个人库异常：' . $exception->getMessage());
            return $this->error();
        }
    }

}
